<?php

namespace App\Http\Controllers;

use App\Http\Requests\ClasssCreate;
use App\Http\Requests\ClasssUpdate;
use App\Models\Classs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;

class ClasssController extends Controller
{

    use ValidatesRequests;

    public function create(ClasssCreate $request){

        $classs = $request->validated();
        $classs['school_id'] = $request->school_id;
        $classs['campus_id'] = $request->campus_id;

        try{
            Classs::create($classs);
            return resSucc(null, "创建成功");
        } catch (\Exception $e) {
            return resFail($e, "创建失败");
        } catch (\Throwable $e) {
            return resFail($e, "创建失败");
        }

    }

    public function update(ClasssUpdate $request){

        $classs = $request->validated();

        try{
            $res = Classs::where('id', $classs['id'])->update($classs);
            return resSucc($res, "更新成功");
        } catch (\Exception $e) {
            return resFail($e, "更新失败");
        } catch (\Throwable $e) {
            return resFail($e, "更新失败");
        }

    }

    public function info(Request $request) {
        $class_id = $request->class_id;
        $school_id =  $request->school_id;
        $campus_id = $request->campus_id;
        $res = Classs::where([
            'school_id' => $school_id,
            'campus_id' => $campus_id,
            'id' => $class_id,
        ])->first();
        return resSucc($res, "班级详情");
    }

    public function list(Request $request){
        $limit = $request->limit ?? 10;
        $res = Classs::where([
            'school_id' => $request->school_id,
            'campus_id' => $request->campus_id
        ])->paginate($limit);
        return resSucc(formatPage($res), "班级列表");
    }

    public function simpleList(Request $request){
        $res = Classs::where([
            'school_id' => $request->school_id,
            'campus_id' => $request->campus_id
        ])->get(['class_name', 'id', 'campus_id', 'school_id', 'status']);
        return resSucc($res, "班级列表");
    }
}
